package com.dbappsecurity.jsbridge.api;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;
import com.dbappsecurity.jsbridge.contract.JsApiThiredContract;
import com.dbappsecurity.jsbridge.entity.jsThirdTitle;
import com.dbappsecurity.jsbridge.webview.CompletionHandler;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsApiThired  {

  private JsApiThiredContract mContract;
  private Handler mHandler = new Handler(Looper.getMainLooper());
  private Gson gson = new Gson();

  public JsApiThired(JsApiThiredContract contract) {
    mContract = contract;
  }


  @JavascriptInterface
  public void scan(Object args, CompletionHandler handler) {
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    Log.i("JsApiToggle", "Arirus scan: " + jsonObject.toString());
    jsThirdTitle thirdTitle = gson.fromJson(jsonObject.toString(), jsThirdTitle.class);
    Log.i("JsApiToggle", "Arirus thirdPartyWeb: " + jsonObject.toString());
    mHandler.post(()->mContract.scan(thirdTitle,handler));
  }

  @JavascriptInterface
  public void thirdPartyWeb(Object args, CompletionHandler handler) {
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    jsThirdTitle thirdTitle = gson.fromJson(jsonObject.toString(), jsThirdTitle.class);
    Log.i("JsApiToggle", "Arirus thirdPartyWeb: " + jsonObject.toString());
    mHandler.post(()->mContract.thirdPartyWeb(thirdTitle,handler));
  }

  @JavascriptInterface
  public void share(Object args, CompletionHandler handler) {
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    Log.i("JsApiToggle", "Arirus share: " + jsonObject.toString());
    mHandler.post(()->mContract.share(jsonObject,handler));
  }

  @JavascriptInterface
  public void location(Object args, CompletionHandler handler) {
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    mHandler.post(()->mContract.location(handler));
  }

  @JavascriptInterface
  public void pushAlias(Object args, CompletionHandler handler){
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    mHandler.post(()-> {
      try {
        mContract.pushAlias(jsonObject.getString("alias"),handler);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    });
  }

  @JavascriptInterface
  public void pushTags(Object args, CompletionHandler handler){
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    try {
      List listdata  = gson.fromJson(jsonObject.get("tags").toString(),List.class);
      mContract.pushTags(listdata,handler);
    }catch (JSONException e){
      e.printStackTrace();
    }
  }

  @JavascriptInterface
  public void pushClean(Object args, CompletionHandler handler){
    mHandler.post(()->mContract.pushClean(handler));
  }

  @JavascriptInterface
  public void chooseImage(Object args, CompletionHandler handler) {
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    Log.i("JsApiToggle", "Arirus chooseImage: " + jsonObject.toString());
    mHandler.post(()->mContract.chooseImage(jsonObject,handler));
  }


  @JavascriptInterface
  public void clickCustom(Object args, CompletionHandler handler) {
    if (args == null) return;
    JSONObject jsonObject = (JSONObject) args;
    Log.i("JsApiToggle", "Arirus clickCustom: " + jsonObject.toString());
  }
}
